<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>打开控制台看输出</h1>
    <script>
      function sayHi() {
        //this 默认指向 Window 浏览器窗口
        // 浏览器的全局函数，其实保存在 window 对象上
        // this 口诀：谁调用的函数，this 就指向谁
        console.log("hello fn 被调用了", this);
      }
      sayHi();

      const obj = {
        name: "张三",
        age: 18,
        sayHi: function () {
          console.log(
            "obj eating:",
            this,
            `你好我叫${this.name},今年${this.age}了`
          );
        },
      };

      obj.sayHi();

      //  =====箭头函数=====
      // 箭头函数没有自己的this 而是共用外层函数的 this
      const sayNo = () => {
        console.log("arrow function", this);
      };
      sayNo();

      const obj2 = {
        name: "李四",
        age: 20,
        sayNo: () => {
          console.log("obj2 eating:", this);
        },
      };
      obj2.sayNo();
    </script>
  </body>
</html>
